package swingFiles;


import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JTextField;
import javax.swing.JButton;

import mainFiles.GmsDB;
import mainFiles.User;

/**
 * The panel that provides the log in screen for users of the gym management system.
 * 
 * @author Emily Linniman
 * @version December 1, 2014
 */
@SuppressWarnings("serial")
public class LogIn extends JPanel {

	/** The field to input the id of the user logging in. */
	private JTextField userIdField;
	
	/** The field to input the password of the user logging in. */
	private JPasswordField passwordField;
	
	/** The frame on which the panel is placed on. */
	private JFrame my_frame;
	
	/** The database object which interacts with the database sytem. */
	private GmsDB db;

	/**
	 * Create the frame.
	 */
	public LogIn(JFrame the_frame) {
		setBackground(Color.ORANGE);
		my_frame = the_frame;
		my_frame.setTitle("Log-In ");
		
		db = new GmsDB();
		setLayout(null);

		
		JLabel welcomeLabel = new JLabel("Welcome to the Gym Management System!");
		welcomeLabel.setBounds(53, 6, 348, 22);
		welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		welcomeLabel.setFont(new Font("Arial", Font.PLAIN, 18));
		add(welcomeLabel);
		
		JLabel userLabel = new JLabel("User ID");
		userLabel.setBounds(113, 71, 51, 18);
		userLabel.setFont(new Font("Arial", Font.PLAIN, 15));
		userLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(userLabel);
		
		userIdField = new JTextField();
		userIdField.setHorizontalAlignment(SwingConstants.CENTER);
		userIdField.setBounds(198, 66, 134, 28);
		add(userIdField);
		userIdField.setColumns(10);
		
		JLabel passwordLabel = new JLabel("Password");
		passwordLabel.setFont(new Font("Arial", Font.PLAIN, 15));
		passwordLabel.setBounds(113, 142, 66, 18);
		add(passwordLabel);
		
		passwordField = new JPasswordField();
		passwordField.setHorizontalAlignment(SwingConstants.CENTER);
		passwordField.setBounds(198, 137, 134, 28);
		add(passwordField);
		passwordField.setColumns(10);
		
		JButton logInButton = new JButton("Log In");
		logInButton.setBackground(Color.WHITE);
		logInButton.setFont(new Font("Arial", Font.PLAIN, 13));
		logInButton.setBounds(166, 200, 80, 29);
		logInButton.addActionListener(new ActionListener() {
			
			//Checks the validity of the log in information
			//If data is valid, directs user to appropriate home page
			//If not valid, displays an error message
			public void actionPerformed(ActionEvent e) {
				boolean success = true;
				String failedElement = null;
				String id = userIdField.getText();
				char[] password = passwordField.getPassword();
				String strPassword = new String(password);
				
				if (id.length() != 9) {
					success = false;
					failedElement = "ID";
				}
				
				List<User> users = db.getUsers(id);
				User currUser = null;
				if (users.isEmpty()) {
					success = false;
					failedElement = "ID";
				} else {
					currUser = users.get(0);
					if (!currUser.getPassword().equals(strPassword)) {
						success = false;
						failedElement = "Password";
					}
				}

				if (success) {
					String role = currUser.getuserRole();
					JPanel newPanel = null;
					if (role.equals("member")) {
						newPanel = new MemberHome(my_frame);
					} else if (role.equals("instructor")) {
						newPanel = new InstructorHome(my_frame);
					} else if (role.equals("staff")) {
						newPanel = new StaffHome(my_frame);
					} else if (role.equals("manager")) {
						newPanel = new ManagerHome(my_frame);
					}
					
					my_frame.setContentPane(newPanel);
					my_frame.validate();
					my_frame.setVisible(true);
					
				} else {
					JOptionPane.showMessageDialog(my_frame, "Log in failed. Please enter a valid " + failedElement, "Error!", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		add(logInButton);
	}
}
